package com.ruoyi.user.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.user.domain.bo.UserHelpGroupBo;
import com.ruoyi.user.domain.vo.UserHelpGroupVo;
import com.ruoyi.user.service.IUserHelpGroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 帮扶小组
 *
 * @author strongme
 * @date 2023-01-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/user/helpGroup")
public class UserHelpGroupController extends BaseController {

    private final IUserHelpGroupService iUserHelpGroupService;

    /**
     * 查询帮扶小组列表
     */
    @GetMapping("/list")
    public TableDataInfo<UserHelpGroupVo> list(UserHelpGroupBo bo, PageQuery pageQuery) {
        return iUserHelpGroupService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出帮扶小组列表
     */
    @Log(title = "帮扶小组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserHelpGroupBo bo, HttpServletResponse response) {
        List<UserHelpGroupVo> list = iUserHelpGroupService.queryList(bo);
        ExcelUtil.exportExcel(list, "帮扶小组", UserHelpGroupVo.class, response);
    }

    /**
     * 获取帮扶小组详细信息
     *
     * @param helpGroupId 主键
     */
    @GetMapping("/{helpGroupId}")
    public R<UserHelpGroupVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long helpGroupId) {
        return R.ok(iUserHelpGroupService.queryById(helpGroupId));
    }

    /**
     * 根据人员信息获取帮扶小组列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/listByUserId/{userId}")
    public R<List<UserHelpGroupVo>> getInfosByUserId(@NotNull(message = "主键不能为空")
                                                     @PathVariable Long userId) {
        return R.ok(iUserHelpGroupService.queryByUserId(userId));
    }

    /**
     * 新增帮扶小组
     */
    @Log(title = "帮扶小组", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserHelpGroupBo bo) {
        return toAjax(iUserHelpGroupService.insertByBo(bo));
    }

    /**
     * 修改帮扶小组
     */
    @Log(title = "帮扶小组", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserHelpGroupBo bo) {
        return toAjax(iUserHelpGroupService.updateByBo(bo));
    }

    /**
     * 删除帮扶小组
     *
     * @param helpGroupIds 主键串
     */
    @Log(title = "帮扶小组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{helpGroupIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] helpGroupIds) {
        return toAjax(iUserHelpGroupService.deleteWithValidByIds(Arrays.asList(helpGroupIds), true));
    }
}
